package org.taobao.ibatiside.model.common.provider;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.util.FeatureMapUtil;
import org.eclipse.emf.edit.provider.AttributeValueWrapperItemProvider;
import org.eclipse.emf.edit.provider.DelegatingWrapperItemProvider;
import org.eclipse.emf.edit.provider.ItemProviderAdapter;
import org.eclipse.emf.edit.provider.WrapperItemProvider;

public class IbatisCommonProvider extends ItemProviderAdapter {

	public IbatisCommonProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}
	public WrapperItemProvider getChildItemProvider(EStructuralFeature eStructuralFeature, int index){
		ChildrenStore store = childrenStoreMap.get(getTarget());
		if (store != null){
			return (WrapperItemProvider) store.get(eStructuralFeature, index);
		}
		return null;
	}
	@Override
	protected Object createWrapper(EObject object, EStructuralFeature feature,
			Object value, int index) {
		 if (!isWrappingNeeded(object)) return value;

		    if (FeatureMapUtil.isFeatureMap(feature))
		    {
		      value = new IbatisFeaturemapProvider((FeatureMap.Entry)value, object, (EAttribute)feature, index, adapterFactory, getResourceLocator());
		    }
		    else if (feature instanceof EAttribute)
		    {
		      value = new AttributeValueWrapperItemProvider(value, object, (EAttribute)feature, index, adapterFactory, getResourceLocator());
		    }
		    else if (!((EReference)feature).isContainment())
		    {
		      value = new DelegatingWrapperItemProvider(value, object, feature, index, adapterFactory);
		    }

		    return value;
	}

}
